package pers.sun.oauthclient.util; /**
 * Created by lenovo on 2016/4/11.
 */

import org.apache.oltu.oauth2.client.OAuthClient;
import org.apache.oltu.oauth2.client.URLConnectionClient;
import org.apache.oltu.oauth2.client.request.OAuthBearerClientRequest;
import org.apache.oltu.oauth2.client.request.OAuthClientRequest;
import org.apache.oltu.oauth2.client.response.OAuthAccessTokenResponse;
import org.apache.oltu.oauth2.client.response.OAuthResourceResponse;
import org.apache.oltu.oauth2.common.OAuth;
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
import org.apache.oltu.oauth2.common.message.types.GrantType;

import java.util.UUID;

public class OAuthClientUtil {
    private String authorizationLocation;
    private String clientId;
    private String clientSecret;
    private String responseType;
    private String scope;
    private String state;
    private GrantType grantType;
    private String tokenLocation;
    private String resourceLocation;
    private String redirectUri;

    public String getAuthorizationLocation() {
        return authorizationLocation;
    }

    public void setAuthorizationLocation(String authorizationLocation) {
        this.authorizationLocation = authorizationLocation;
    }

    public String getClientId() {
        return clientId;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    public String getClientSecret() {
        return clientSecret;
    }

    public void setClientSecret(String clientSecret) {
        this.clientSecret = clientSecret;
    }

    public String getResponseType() {
        return responseType;
    }

    public void setResponseType(String responseType) {
        this.responseType = responseType;
    }

    public String getScope() {
        return scope;
    }

    public void setScope(String scope) {
        this.scope = scope;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public GrantType getGrantType() {
        return grantType;
    }

    public void setGrantType(GrantType grantType) {
        this.grantType = grantType;
    }

    public String getTokenLocation() {
        return tokenLocation;
    }

    public void setTokenLocation(String tokenLocation) {
        this.tokenLocation = tokenLocation;
    }

    public String getResourceLocation() {
        return resourceLocation;
    }

    public void setResourceLocation(String resourceLocation) {
        this.resourceLocation = resourceLocation;
    }

    public String getRedirectUri() {
        return redirectUri;
    }

    public void setRedirectUri(String redirectUri) {
        this.redirectUri = redirectUri;
    }

    /**
     * 构造函数，为属性赋值
     */
    public OAuthClientUtil(String clientId, String clientSecret, String redirectUri) {
        //以下接口访问地址是测试服务器的访问地址，当正式对接国家平台时，应该为注释的地址
        this.resourceLocation = "https://218.249.73.248/OAuthServer/oauth2/resource/userinfo.ins" ;
        this.tokenLocation = "https://218.249.73.248/OAuthServer/oauth2/access_token.ins";
        this.authorizationLocation = "http://218.249.73.248:8080/OAuthServer/oauth2/authorize.ins";
        //这里是正式对接时的接口访问地址，注意正式对接时的client_id和client_secret是注册后获得的注册信息
        /*this.resourceLocation = "https://218.249.73.245/instru_war/oauth2/resource/userinfo.ins" ;
        this.tokenLocation = "https://218.249.73.245/instru_war/oauth2/access_token.ins";
        this.authorizationLocation = "http://218.249.73.245/instru_war/oauth2/authorize.ins";*/

        this.clientId = clientId;
        this.clientSecret = clientSecret;
        this.responseType = "code";  //默认
        this.scope = "read"; //默认
        this.state = UUID.randomUUID().toString(); //默认
        this.grantType = GrantType.AUTHORIZATION_CODE;
        this.redirectUri = redirectUri;//单位平台的回调地址
    }

    /**
     * 生成authorize请求连接测试
     * 之后可用如下语句重定向用户到authorize接口
     * httpServletResponse.sendRedirect(request.getLocationUri());
     *
     * @return authorize接口访问uri
     * @throws OAuthSystemException
     */
    public String AuthzTest() throws OAuthSystemException {
        OAuthClientRequest request = OAuthClientRequest
                .authorizationLocation(this.authorizationLocation)
                .setClientId(this.clientId)
                .setRedirectURI(this.redirectUri)
                .setResponseType(this.responseType)
                .setScope(this.scope)
                .setState(this.state)
                .buildQueryMessage();
        return request.getLocationUri();
    }

    /**
     * 请求access_token接口，并提取接口返回结果中的token
     * @param code 大仪平台返回的authrize_code
     * @return 返回访问令牌token
     * @throws OAuthSystemException
     * @throws OAuthProblemException
     */
    public String TokenTest(String code) throws OAuthSystemException, OAuthProblemException {
        OAuthClientRequest request = OAuthClientRequest
                .tokenLocation(this.tokenLocation)
                .setClientId(this.clientId)
                .setClientSecret(this.clientSecret)
                .setGrantType(this.grantType)
                .setCode(code)
                .setRedirectURI(this.redirectUri)
                .buildQueryMessage();
        //此为调用access_token接口时请求的url
        String url = request.getLocationUri();
        //URLConnectionClient为HttpClient的一个简单实现，可以替换为 HttpsClient，实现https连接
        OAuthClient oAuthClient = new OAuthClient(new HttpsClient());
        OAuthAccessTokenResponse response = oAuthClient.accessToken(request, OAuth.HttpMethod.POST);
        return response.getAccessToken();
    }

    /**
     *  发起resource请求，并返回JSON字符串
     * @param accesstoken  大仪平台返回的访问令牌
     * @return  返回大仪平台用户相关信息
     * @throws OAuthSystemException
     * @throws OAuthProblemException
     */
    public String ResourceTest(String accesstoken) throws OAuthSystemException, OAuthProblemException {
        OAuthClientRequest request = new OAuthBearerClientRequest(this.resourceLocation)
                .setAccessToken(accesstoken)
                .buildQueryMessage();
        //此为调用resource接口时请求的url
        String url = request.getLocationUri();
        //URLConnectionClient为HttpClient的一个简单实现，可以替换为 HttpsClient，实现https连接
        OAuthClient oAuthClient = new OAuthClient(new HttpsClient());
        OAuthResourceResponse response = oAuthClient.resource(request, OAuth.HttpMethod.GET, OAuthResourceResponse.class);
        return response.getBody();
    }

}
